#################################################################
# r_pvalues.R 							#
# 								#
# Calculates correlation between genotype and expression level	#
#								#
# (c) Niklas Malmqvist & Jesper Gådin, Feb 2011			#
#								#
#################################################################

args <- commandArgs(TRUE)		

genotype <- read.table(file="genotype.txt",head=F,sep=",",fill=TRUE)
expression <- read.table(file="explvl.txt",head=F,sep=",",fill=TRUE)

geno1<-as.matrix(genotype)
geno2<-as.vector(geno1[1,])
geno2<-as.numeric(geno2)

exp1<-as.matrix(expression)
exp2<-as.vector(exp1[1,])
exp2<-as.numeric(exp2)

snp <- args[1]
probeid <- args[2]
genesym <- args[3]
tissuetype <- args[4]

write(geno2, file="genot_r.txt")
write(exp2, file="exp_r.txt")

model1 <- lm(geno2~exp2) 
p<-summary(model1)[["coefficients"]]["exp2","Pr(>|t|)"]

c(png(filename="/home/niklas/www/KI-webapp-Prototyp/pv_graphs/temp.png", width=500, height=500), plot(geno2,exp2,xlab="",ylab="expression"), mtext(paste("P-value:",signif(p,3))), mtext(paste("Gene:",genesym),padj=-1.25), mtext(paste("SNP:",snp),padj=-2.5), mtext(paste("Tissue:",tissuetype),padj=-3.75))
c(pdf("/home/niklas/www/KI-webapp-Prototyp/pv_graphs/temp.pdf"), plot(geno2,exp2,xlab="",ylab="expression"), mtext(paste("P-value:",signif(p,3))), mtext(paste("Gene:",genesym),padj=-1.25), mtext(paste("SNP:",snp),padj=-2.5), mtext(paste("Tissue:",tissuetype),padj=-3.75))

dev.off()
